package com.siyu.rehearsal.grpc;

import com.itf.calc.proto.ItfModelProto;
import com.itf.calc.proto.ItfServiceGrpc;
import com.siyu.rehearsal.domain.SwwmResult;
import com.siyu.rehearsal.ttdimension.domain.Dimension;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;



@Service
public class ItfService extends ItfServiceGrpc.ItfServiceImplBase {


    public void itfCalc(ArrayList<SwwmResult> swwmResults,Dimension ttDimension){

        ArrayList<Double> qt = new ArrayList<>();
        ArrayList<String> tms = new ArrayList<>();
        int a = 0;
        for (SwwmResult swwmResult : swwmResults) {
            if (swwmResult.getTm() != null) {
                if (a == 0) {
                    tms.add(swwmResult.getTm());
                }
                a++;
                if (a == 15) {
                    a = 0;
                }
            }
            if (swwmResult.getOutletFlow() != null) {
                qt.add(swwmResult.getOutletFlow().doubleValue());
            }
        }
        // 开始时间处理
        String startTimeStr = tms.get(0);
        String endTimeStr = tms.get(tms.size()-1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        // 将字符串转换为LocalDateTime对象
        LocalDateTime endTime = LocalDateTime.parse(endTimeStr, formatter);

        // 减去一个小时
        LocalDateTime adjustedTime = endTime.plusHours(1);

        // 将调整后的时间转换回字符串
        String adjustedTimeString = adjustedTime.format(formatter);


        // crtl配置文件修改
        CSVTimeReplacer.updateCtrlCsv(startTimeStr,adjustedTimeString,"./cfdata/"+ttDimension.getCtrlPath(),ttDimension.getFilePath()+ttDimension.getCtrlPath());

        try {
            ArrayList<ArrayList<Double>> arrayLists = new ArrayList<>();
            int j = 0;
            for (int i = 0; i < qt.size(); i++) {
                if (i<=14){
                    ArrayList<Double> list = new ArrayList<>();
                    list.add(qt.get(i));
                    arrayLists.add(list);
                }else {
                    if (j>14){
                        j=0;
                    }
                    arrayLists.get(j).add(qt.get(i));
                    j++;
                }
            }

            ArrayList<ArrayList<Double>> aaaa = new ArrayList<>();
            for (int k = 0; k < arrayLists.size(); k++) {
                ArrayList<Double> list = arrayLists.get(k);
                ArrayList<Double> qts = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    for (int l = 0; l < 12; l++) {
                        qts.add(list.get(i));
                    }
                }
                qts.add(0.0);
                aaaa.add(qts);
            }
            // 时间序列分钟处理
            ArrayList<String> chulimin = TimeGenerator.chulimin(startTimeStr, adjustedTimeString);
            WriteToCSV.write15DataToCSV(aaaa,chulimin,ttDimension.getFilePath()+ttDimension.getI17Path());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 清理resultgeo文件夹生成的geo文件以及清除resultgeo.zip文件
        String ctrlpath = ttDimension.getFilePath() + ttDimension.getCtrlPath();
        String outputPath = ttDimension.getOutputPath();
        String filePath = ttDimension.getFilePath();
        RunShellScript.runshell(ctrlpath,outputPath,filePath);

        try {
            DirectoryWatcher.resultasc(ttDimension.getOutputPath(),tms.size(),ttDimension.getOutgeoPath());
        } catch (Exception e) {
            System.out.println("发生错误:  "+e);
            e.printStackTrace();
        }


    }

}
